import { _decorator, Component, Node } from 'cc';
import MagicItem from '../data/MagicItem';
import PokerItem from '../data/PokerItem';
import { listenerClick } from '../core/EventHelper';
import { Label } from 'cc';
const { ccclass, property } = _decorator;

export type Callback = {
    shop?: (poker: MagicItem) => boolean,
    sell?: (poker: MagicItem) => void,
    handle?: (poker: MagicItem) => boolean;
}

@ccclass('PokerDetailDialog')
export class PokerDetailDialog {

    // root
    @property(Node)
    root: Node = null!

    // 使用
    @property(Node)
    handle: Node = null!

    // 购入
    @property(Node)
    shop: Node = null!

    // 卖出
    @property(Node)
    sell: Node = null!

    // close
    @property(Node)
    close: Node = null!

    // title
    @property(Label)
    title: Label = null!

    // desc
    @property(Label)
    desc: Label = null!

    protected onLoad(): void {
        this.handle.active = false;
        this.shop.active = false;
        this.sell.active = false;
        this.title.node.active = false;
        listenerClick(this.close, this.dialogClose, this);
    }

    dialogClose() {
        this.root.destroy()
    }

    showPoker(poker: MagicItem | PokerItem, callback?: Callback) {
        console.log(' showPoker', 2, this.handle.active)
        if (poker instanceof MagicItem) {
            const magic = poker.magic;
            if (magic.name) {
                this.title.node.active = true;
                this.title.string = magic.name;
            }
            this.desc.string = magic.desc;

            console.log(' showPoker', 1)
            if (callback && callback.handle) {
                console.log(' showPoker', 2, this.handle.active)
                this.handle.active = true;
                console.log(' showPoker', 3, this.handle.active)
                const handle = callback.handle;
                listenerClick(this.handle, () => {
                    handle(poker) && this.dialogClose();
                })
            }
        } else {

        }
    }

    updateTexture(poker: PokerItem) {

    }

    update(deltaTime: number) {

    }
}

